import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

public class redistest {
    public static void main(String[] args) {
        Set<HostAndPort> nodes=new LinkedHashSet<>();
        //一般选用slaveof从IP+端口进行增删改查，不用master
        /*nodes.add(new HostAndPort("127.0.0.1",6379));
        nodes.add(new HostAndPort("127.0.0.1",6380));
        nodes.add(new HostAndPort("127.0.0.1",6381));*/
//        nodes.add(new HostAndPort("81.68.113.90",5001));
        nodes.add(new HostAndPort("81.68.113.90",5002));
//        nodes.add(new HostAndPort("81.68.113.90",5003));
//        nodes.add(new HostAndPort("81.68.113.90",5004));
//        nodes.add(new HostAndPort("81.68.113.90",5005));
//        nodes.add(new HostAndPort("81.68.113.90",5006));


        //Jedis连接池配置
        JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
        //最大空闲连接数,默认8个
        jedisPoolConfig.setMaxIdle(500);
        //最大连接数,默认8个
        jedisPoolConfig.setMaxTotal(500);
        //设置最小空闲连接数,默认0
        jedisPoolConfig.setMinIdle(0);
        // 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1
        jedisPoolConfig.setMaxWaitMillis(2000);//设置2秒
        //对拿到的connection进行validateObject校验
        jedisPoolConfig.setTestOnBorrow(true);
        //未设置auth Password
        //JedisCluster jedis = new JedisCluster(nodes, jedisPoolConfig);
        //设置auth Password
        JedisCluster jedis = new JedisCluster(nodes, 5000, 3000, 10, "123456", new JedisPoolConfig());

        jedis.set("mykey", "测试");

        System.out.println(jedis.get("mykey"));
    }

    @Test //java 连接 Redis 集群
    public void testjiqun(){
        //创建Set<HashAndPort> node
        //1.先创建一个集合，用来装我们集群的节点，包含IP+端口
        //尽可能的多写几个，防止有机器宕机了，然后我们无法连接上集群；
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("81.68.113.90",5001));
        nodes.add(new HostAndPort("81.68.113.90",5002));
        nodes.add(new HostAndPort("81.68.113.90",5003));
        nodes.add(new HostAndPort("81.68.113.90",5004));
        nodes.add(new HostAndPort("81.68.113.90",5005));
        nodes.add(new HostAndPort("81.68.113.90",5006));
        //创建JedisCluster对象
        //写完了集合之后，把他们封装到JedisCluster对象之中；相当于传参；
        //进行传递连接；
        JedisCluster jedisCluster = new JedisCluster(nodes);

        //常规的指令都有，但是跨节点的操作就没有，比如没有 keys *
        System.out.print(jedisCluster.set("name","3333333"));
        System.out.print(jedisCluster.set("gender","sdfsd"));
        System.out.print(jedisCluster.set("age","200000"));

        System.out.print(jedisCluster.get("name"));
        System.out.print(jedisCluster.get("gender"));
        System.out.print(jedisCluster.get("age"));

    }
    @Test
    public void re(){
        
    }
}
